//
//  LeftViewController.m
//  VEW
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 Y团. All rights reserved.
//

#import "LeftViewController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "CollectTableViewController.h"
#import "DataBaseManager.h"
#import "SDWebImageManager.h"

/** 左抽屉的内容 */


@interface LeftViewController ()<UIAlertViewDelegate>

{
    float filSize;
}


@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageview.image = [UIImage imageNamed:@"星空.jpg"];

    [self.view addSubview:imageview];
    
    UITableView *tableview = [[UITableView alloc] init];
    self.tableview = tableview;
    tableview.frame = self.view.bounds;
    tableview.dataSource = self;
    tableview.delegate  = self;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableview];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - uitableview delegate -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row==0)
    {
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    LoginViewController *loginVC= [[LoginViewController alloc] init];
        CollectTableViewController *collectTVC=[[CollectTableViewController alloc]init];
    [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
    collectTVC.hidesBottomBarWhenPushed=YES;
    [tempAppDelegate.homeNC pushViewController:collectTVC animated:YES];
    }
    if(indexPath.row==1)
    {
        NSString *str=[NSString stringWithFormat:@"\n 是否清理缓存?"];
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alertView show];
    }
    if(indexPath.row == 2)
    {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"关于我们" message:@"\n本应用所有内容收集整理均来自互联网,访问者可将本网站内容用于学习欣赏,以及其他非商业用途,并同时遵循本网站合法权利。\n\n版本V1.0.0" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alertView show];
    }
    if (indexPath.row == 4) {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"检查更新" message:@"\n当前为最新版本\r\n 版本V1.0.0" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
}

//alertview点击
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self clear];
    }
}
/**清除缓存 */
- (void)clear
{
    
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       NSString * cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                       NSLog(@"%@",cachPath);
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }

                   });
    
}


#pragma mark - uitable datasource -

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
    }
    
    NSArray *titles = @[@"我的收藏", @"清理缓存", @"关于我们", @"夜间模式",@"版本更新"];
    NSArray *images = @[@"左收藏", @"左清理", @"左我们", @"左月亮",@"左个人"];
    cell.textLabel.text = titles[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];

    if (indexPath.row==3) {
        UISwitch *nightSwitch=[[UISwitch alloc]initWithFrame:CGRectMake(cell.size.width*0.5, cell.size.height *0.3, 30, 50)];
        [nightSwitch addTarget:self action:@selector(night:) forControlEvents:UIControlEventValueChanged];
        [nightSwitch setOn:NO animated:YES];
        [cell addSubview:nightSwitch];
    }
    
    return cell;
}

// 夜间模式
-(void)night:(UISwitch *)swit
{
    if ([swit isOn]) {
        self.view.window.alpha=0.5;
    }
    else{
        self.view.window.alpha=1;
    }
}

//设置区头
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 180;
}

//区头颜色
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.bounds.size.width, 180)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}


@end
